
let RouterUtils = {};

//是否开发模式
RouterUtils.isDev = false;

RouterUtils.oneOf = function (ele, targetArr) {
  if (targetArr === undefined || targetArr == null || targetArr.length < 0)
    return false
  if (targetArr.indexOf(ele) >= 0) {
    return true
  } else {
    return false
  }
}

RouterUtils.showThisRoute = function (itAccess, currentAccess) {
  /*if (typeof itAccess === 'object' && itAccess.isArray()) {
      return RouterUtils.oneOf(currentAccess, itAccess);
  } else {
      return itAccess === currentAccess;
  }*/

  return RouterUtils.oneOf(itAccess, currentAccess)
}

RouterUtils.getRouterObjByName = function (routers, name) {
  let routerObj = {}
  routers.forEach(item => {
    if (item.name === 'otherRouter') {
      item.children.forEach((child, i) => {
        if (child.name === name) {
          routerObj = item.children[i]
        }
      })
    } else {
      item.children.forEach(item => {
        if (item.children.length === 1) {
          if (item.children[0].name === name) {
            routerObj = item.children[0]

          }
        } else {
          item.children.forEach((child, i) => {
            if (child.name === name) {
              //console.log("找到了：" + name);
              routerObj = item.children[i]
            }
          })
        }
      })
    }
  })
  return routerObj
}

RouterUtils.handleTitle = function (vm, item) {
  if (typeof item.title === 'object') {
    return vm.$t(item.title.i18n)
  } else {
    return item.title
  }
}

RouterUtils.setCurrentPath = function (vm, name) {
  let title = ''
  let isOtherRouter = false
  vm.$store.state.routers.forEach(topMenu => {
    if (topMenu.name === 'otherRouter') {
      topMenu.children.forEach(item => {
        if (item.name === name) {
          isOtherRouter = true
          title = RouterUtils.handleTitle(vm, item)
        }
      })
    } else {
      topMenu.children.forEach(item => {
        if (item.children.length === 1) {
          if (item.children[0].name === name)
            title = RouterUtils.handleTitle(vm, item)
        } else {
          item.children.forEach(child => {
            if (child.name === name)
              title = RouterUtils.handleTitle(vm, child)
          })
        }
      })
    }
  })


  let currentPathArr = []
  if (name === 'home_index') {
    currentPathArr = [
      {
        title: RouterUtils.handleTitle(vm, RouterUtils.getRouterObjByName(vm.$store.state.routers, 'home_index')),
        path: '',
        name: 'home_index'
      }
    ]
  } else if ((name.indexOf('_index') >= 0 || isOtherRouter) && name !== 'home_index') {
    currentPathArr = [
      {
        title: RouterUtils.handleTitle(vm, RouterUtils.getRouterObjByName(vm.$store.state.routers, 'home_index')),
        path: '/home',
        name: 'home_index'
      },
      {
        title: title,
        path: '',
        name: name
      }
    ]
  } else {
    let currentPathObj = null
    vm.$store.state.routers.forEach(topMenu => {
      if (topMenu.name !== 'otherRouter') {
        topMenu.children.forEach(item => {
          if (item.children.length <= 1) {
            if (item.children[0].name === name) {
              currentPathObj = item
            }
          } else {
            let i = 0
            let childArr = item.children
            let len = childArr.length
            while (i < len) {
              if (childArr[i].name === name) {
                currentPathObj = item
              }
              i++
            }
          }

        })
      }

    })
    if (currentPathObj && currentPathObj.children.length <= 1 && currentPathObj.name === 'home') {
      currentPathArr = [
        {
          title: '首页',
          path: '',
          name: 'home_index'
        }
      ]
    } else if (currentPathObj && currentPathObj.children.length <= 1 && currentPathObj.name !== 'home') {
      currentPathArr = [
        {
          title: '首页',
          path: '/home',
          name: 'home_index'
        },
        {
          title: currentPathObj.title,
          path: '',
          name: name
        }
      ]
    } else if (currentPathObj) {
      let childObj = currentPathObj.children.filter((child) => {
        return child.name === name
      })[0]
      currentPathArr = [
        {
          title: '首页',
          path: '/home',
          name: 'home_index'
        },
        {
          title: currentPathObj.title,
          path: '',
          name: currentPathObj.name
        },
        {
          title: childObj.title,
          path: currentPathObj.path + '/' + childObj.path,
          name: name
        }
      ]
    }
  }
  vm.$store.commit('setCurrentPath', currentPathArr)
  return currentPathArr
}

RouterUtils.openNewPage = function (vm, name, argu, query) {
  let pageOpenedList = vm.$store.state.pageOpenedList
  let openedPageLen = pageOpenedList.length
  let i = 0
  let tagHasOpened = false
  while (i < openedPageLen) {
    if (name === pageOpenedList[i].name) {  // 页面已经打开
      vm.$store.commit('pageOpenedList', {
        index: i,
        argu: argu,
        query: query
      })
      tagHasOpened = true
      break
    }
    i++
  }
  if (!tagHasOpened) {
    let tag = vm.$store.state.tagsList.filter((item) => {
      if (item.children) {
        return name === item.children[0].name
      } else {
        return name === item.name
      }
    })
    tag = tag[0]
    tag = tag.children ? tag.children[0] : tag
    if (argu) {
      tag.argu = argu
    }
    if (query) {
      tag.query = query
    }
    vm.$store.commit('increateTag', tag)
  }
  vm.$store.commit('setCurrentPageName', name)
}

RouterUtils.toDefaultPage = function (routers, name, route, next) {
  let len = routers.length
  let i = 0
  let notHandle = true
  while (i < len) {
    if (routers[i].name === name && routers[i].redirect === undefined) {
      if (routers[i].hasOwnProperty('children') && routers[i].name !== 'otherRouter') {
        let menu = routers[i].children
        menu.forEach(item => {
          if (item.name === name && item.redirect === undefined) {
            route.replace({
              name: item.children[0].name
            })
          }
        })
      } else {
        route.replace({
          name: routers[i].children[0].name
        })
      }
      notHandle = false
      next()
      break
    }
    i++
  }
  if (notHandle) {
    next()
  }
}

export {RouterUtils}
